package cn.gson.oasys.controller.excel;

import cn.gson.oasys.model.dao.excel.WagesDao;
import cn.gson.oasys.model.dao.user.UserDao;
import cn.gson.oasys.model.entity.excel.EasyExcelUtil;
import cn.gson.oasys.model.entity.excel.ExcelModelWage;
import cn.gson.oasys.model.entity.excel.Wage;
import cn.gson.oasys.model.entity.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/")
public class WagesController {


    @Autowired
    private UserDao uDao;

    @Autowired
    private WagesDao wagesDao;

    @RequestMapping(value="addwages",method = RequestMethod.POST)
    public String usereditpost(Wage user,
                               Model model) {
        Wage m=user;
        if(user.getId()==null){
            wagesDao.save(user);
        }else{
            Wage entity = wagesDao.findOne(user.getId());
            entity.setBbss(m.getBbss());
            entity.setBtsx(m.getBtsx());
            entity.setFxny(m.getFxny());
            entity.setGh(m.getGh());
            entity.setGs(m.getGs());
            entity.setGz(m.getGz());
            entity.setMq(m.getMq());
            entity.setPqgs(m.getPqgs());
            entity.setQt(m.getQt());
            entity.setSfje(m.getSfje());
            entity.setTs(m.getTs());
            entity.setXm(m.getXm());
            entity.setYbss(m.getYbss());
            entity.setYbsx(m.getYbsx());
            entity.setYfje(m.getYfje());
            entity.setZss(m.getZss());
            System.out.println("WagesController.usereditpost");
            wagesDao.save(entity);
        }
        model.addAttribute("success",1);
        return "/gongzi";
    }

    //导入excel
    @RequestMapping(value = "gzimp", method = {RequestMethod.GET, RequestMethod.POST})
    public String excelImport(HttpServletRequest request, MultipartFile file) throws Exception {
        List<Object> list = EasyExcelUtil.readExcel(file, new ExcelModelWage(), 1, 1);
        if (list != null && list.size() > 0) {
            for (Object o : list) {
                ExcelModelWage m = (ExcelModelWage) o;
                System.out.println("工号："+m.getGh());
                Wage entity=new Wage();
                entity.setBbss(m.getBbss());
                entity.setBtsx(m.getBtsx());
                entity.setFxny(m.getFxny());
                entity.setGh(m.getGh());
                entity.setGs(m.getGs());
                entity.setGz(m.getGz());
                entity.setMq(m.getMq());
                entity.setPqgs(m.getPqgs());
                entity.setQt(m.getQt());
                entity.setSfje(m.getSfje());
                entity.setTs(m.getTs());
                entity.setXm(m.getXm());
                entity.setYbss(m.getYbss());
                entity.setYbsx(m.getYbsx());
                entity.setYfje(m.getYfje());
                entity.setZss(m.getZss());
                //List<WagesModelEntity> entities=new ArrayList<>();
                //entities.add(entity);
                wagesDao.save(entity);

                User user=uDao.findOneUser(m.getGh());
                if(Objects.isNull(user)){
                    //新的用户
                    user=new User();
                    user.setUserName(m.getGh());
                    user.setUserTel(m.getGh());
                    user.setPassword(m.getGh());
                    user.setIsLock(0);
                   // user.setRole(rDao.findOne(5L));
//                    user.setEamil("cs");
//                    user.setAddress("cs");
//                    user.setUserSign("cs");
//                    user.setSchool("cs");
//                    user.setIdCard("cs");
//                    user.setBank("cs");
//                    user.setUserEdu("cs");
//                    user.setRealName("cs");
                    uDao.save(user);
                }
            }
        }
        return "/gongzi";
    }
}